<?php
session_start();

class Authentication{
    public static function auth($pw)
    {
        if (isset($_SESSION['user_pw'])) {
            if ($_SESSION['user_pw'] == $pw) {
                if (isset($_SESSION['user_fingerprint'])) {
                    if ($_SESSION['user_fingerprint'] == self::getBrowserFingerprint()) {
                        return true;
                    }
                }
            }
        }
    }

    // 生成浏览器指纹
    public static function getBrowserFingerprint(){
        $userAgent = $_SERVER['HTTP_USER_AGENT']; // 获取用户代理
        $acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE']; // 获取语言偏好
        //获取IP地址
        $ip = $_SERVER['REMOTE_ADDR'];
        //获取Cookie
        $cookie = $_SERVER['HTTP_COOKIE'];
        //生成指纹
        $fingerprint = md5($userAgent . $acceptLanguage . $ip . $cookie);
        return $fingerprint;
    }
}
?>
